package C051;

import java.util.Scanner;

/**
 * ClassName: C03
 * Package: C051
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/3/10 00:29
 * @Version 1.0
 * @Since 1.0
 */
public class C03 {
    
    static int n;
    static int MAXLEN = 100001;
    static long[] arr = new long[MAXLEN];
    static int max;
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        max = Integer.MIN_VALUE;
        n = scan.nextInt();
        for (int i = 1; i <= n; i++) {
            arr[i] = scan.nextInt();
            max = (int) Math.max(arr[i], max);
        }
        System.out.println(process());
    }

    // 处理方法
    private static int process() {
       int l = 1, r = max, mid;
       int ans = 0;
       while(l <= r) {
           mid = l + ((r - l) >> 1);
           if(process2(mid)) {
               ans = mid;
               r = mid - 1;
           } else {
               l = mid + 1;
           }
       }
       return ans;
    }

    // 判断以mid能量为起点，是否能达到N建筑
    private static boolean process2(int energy) {
        for (int i = 1; i <= n; i++) {
            if(arr[i] <= energy) {
                energy += energy - arr[i];
            }else {
                energy -= arr[i] - energy;
            }
            if(energy < 0) {
                return false;
            } else if(energy >= max) {
                return true;
            }
        }
        return true;
    }
}
